package ru.nsu.fit.client.core.commands;

import ru.nsu.fit.client.core.log_manager.realization.KernelLogManager;
import ru.nsu.fit.client.core.response.KernelResponse;
import ru.nsu.fit.client.core.soap.get_user_stat.GetUserStat;
import ru.nsu.fit.client.core.soap.get_user_stat.GetUserStatOutput;

public class GetUserStatCommand implements  IClientCommand {
    private final long userSessionId;
    private final String serverUrl;

    public GetUserStatCommand(final String serverUrl, final long userSessionId)
    {
        this.userSessionId = userSessionId;
        this.serverUrl = serverUrl;
    }

    @Override
    public KernelResponse execute() {
        try
        {
            KernelLogManager.print("Getting user statistics...");
            GetUserStatOutput output = GetUserStat.execute(serverUrl, userSessionId);

            KernelLogManager.print("Getting user statistics - got response!");
            KernelResponse response = new KernelResponse(false);
            response.setSuccessful(output.isSuccessful());
            response.setErrorText(output.getErrorMessage());
            response.setData(output.getStatistics());

            return response;
        }
        catch (Exception e)
        {
            KernelLogManager.printf("Getting user statistics - web service error: %s", e.getMessage());
            KernelResponse response = new KernelResponse(false);
            response.setErrorText(e.getMessage());
            return response;
        }
    }
}
